import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/common/entitys/home_banner_entity.dart';
import 'package:flutter_ducafecat_news/common/entitys/home_newtruck_entity.dart';
import 'package:flutter_ducafecat_news/common/utils/utils.dart';
import 'package:flutter_ducafecat_news/common/widgets/widgets.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_ducafecat_news/base/base_mvp_presenter.dart';
import 'package:flutter_ducafecat_news/base/base_mvp_widget.dart';
import 'package:flutter_ducafecat_news/base/base_mvp_view.dart';
import 'package:flutter_ducafecat_news/pages/home/home_presenter.dart';
import 'package:flutter_ducafecat_news/pages/home/home_view.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class HomePage extends BaseWidget {
  @override
  BaseWidgetState<BasePresenter<BaseMvpView>, BaseWidget> getState() {
    // TODO: implement getState
    return _homePageState();
  }
}

class _homePageState extends BaseWidgetState<HomePresenter, HomePage>
    implements HomeView {
  var list = [];
  List<HomeNewtruckData> homeNewtruckDatas = [];
  List<HomeBannerData> bannerList = [];
  List<String> truckTypeList = [
    "载货车",
    "载货车",
    "载货车",
    "载货车",
    "载货车",
    "载货车",
    "载货车",
    "载货车",
    "载货车"
  ];
  EasyRefreshController _controller = EasyRefreshController();
  @override
  Widget buildWidget(BuildContext context) {
    // TODO: implement buildWidget
    return Container(
        child: EasyRefresh(
      controller: _controller,
      header: ClassicalHeader(),
      enableControlFinishRefresh: true,
      onRefresh: () async {
        mPresenter.getBannerList();
      },
      child: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            _bulidBannerWidget(),
            _bulidGridWdiget(),
            _buildTruckTypeWidget(),
            _buildShopImageWidget(),
            _buildListWidget()
          ],
        ),
      ),
    ));
  }

  Widget _bulidBannerWidget() {
    return Container(
      height: 375.w,
      child: Swiper(
          itemBuilder: (BuildContext context, int index) {
            return new Image.network(
              bannerList[index].imgurl,
              fit: BoxFit.fill,
            );
          },
          itemCount: bannerList.length,
          pagination: new SwiperPagination(),
          loop:true,
          index: 0,
          autoplay:true,
          duration: 2000),
    );
  }

  Widget _bulidGridWdiget() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(top: 15.h, bottom: 15.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          Column(
            children: <Widget>[
              Image(
                  image: AssetImage("assets/images/home-def-sell.png"),
                  width: 50.0),
              SizedBox(
                height: 5.h,
              ),
              Text("频道")
            ],
          ),
          Column(
            children: <Widget>[
              Image(
                  image: AssetImage("assets/images/home-def-sell.png"),
                  width: 50.0),
              SizedBox(
                height: 5.h,
              ),
              Text(
                "频道",
              )
            ],
          ),
          Column(
            children: <Widget>[
              Image(
                  image: AssetImage("assets/images/home-def-sell.png"),
                  width: 50.0),
              SizedBox(
                height: 5.h,
              ),
              Text("频道")
            ],
          ),
          Column(
            children: <Widget>[
              Image(
                  image: AssetImage("assets/images/home-def-sell.png"),
                  width: 50.0),
              SizedBox(
                height: 5.h,
              ),
              Text("频道")
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildListWidget() {
    return Container(
        child: Column(
      children: homeNewtruckDatas.map((item) {
        return Column(
          children: <Widget>[
            _buildListItemWidget(item),
            Divider(height: 1),
          ],
        );
      }).toList(),
    ));
  }

  Widget _buildListItemWidget(HomeNewtruckData homeNewtruckData) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(25.h),
      child: Row(
        children: <Widget>[
          ImageCached(
            homeNewtruckData.imgThumbnail,
            width: 200,
            height: 150,
          ),
          SizedBox(
            width: 20.w,
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Text(
                  homeNewtruckData.titleStr.split("\$\$")[0],
                  style: TextStyle(
                    fontSize: 30.sp,
                  ),
                  maxLines: 2,
                ),
                SizedBox(
                  height: 20.h,
                ),
                Text(homeNewtruckData.brandName,
                    style: TextStyle(fontSize: 26.sp, color: Colors.grey[400])),
                SizedBox(
                  height: 20.h,
                ),
                Row(
                  children: <Widget>[
                    Text(homeNewtruckData.price + "万",
                        style:
                            TextStyle(fontSize: 30.sp, color: Colors.red[400])),
                    Spacer(),
                    Container(
                        decoration: BoxDecoration(
                            color: Colors.deepOrangeAccent,
                            borderRadius:
                                BorderRadius.all(Radius.circular(15.w))),
                        padding: EdgeInsets.fromLTRB(10.w, 5.w, 10.w, 5.w),
                        child: Text(
                          "可议价",
                          style:
                              TextStyle(fontSize: 20.sp, color: Colors.white),
                        )),
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _buildTruckTypeWidget() {
    return Container(
        color: Colors.white,
        margin: EdgeInsets.only(top: 10.h),
        child: GridView.count(
            //水平子Widget之间间距
            // crossAxisSpacing: 10.0,
            //垂直子Widget之间间距
            // mainAxisSpacing: 30.0,
            //GridView内边距
            padding: EdgeInsets.all(10.0),
            //一行的Widget数量
            crossAxisCount: 4,
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            //子Widget宽高比例
            childAspectRatio: 2.0,
            //子Widget列表
            children: truckTypeList
                .map((item) => _buildTruckTypeItem(item))
                .toList()));

//    Container(
//      child: ListView.separated(
//
//        itemBuilder: (context, index) {
//          return _buildListItemWidget(index);
//        },
//        scrollDirection: Axis.vertical,
//        separatorBuilder: (BuildContext context, int index) {
//          return Container(height: 1, color: Colors.grey[333]);
//        },
//        itemCount: homeNewtruckDatas.length,
//        shrinkWrap: true,
//        physics: NeverScrollableScrollPhysics(),
//      ),
//    );
  }

  Widget _buildTruckTypeItem(item) {
    return Container(
      alignment: Alignment.center,
      child: Text(
        item,
      ),
    );
  }

  Widget _buildShopImageWidget() {
    return Container(
      child: Image(
        image: AssetImage("assets/images/home-shop-image.png"),
        fit: BoxFit.fitWidth,
      ),
    );
  }

  @override
  BasePresenter<BaseMvpView> initPresenter() {
    // TODO: implement initPresenter
    return HomePresenter();
  }

  @override
  void showSuccess(data) {
    // TODO: implement showSuccess
  }

  @override
  void initData() {
    // TODO: implement initData
    setAppBarText("首页");
    setOtherHeight(getBottomNavigationBarHeight(context)+2
    );
    mPresenter.getBannerList();
    mPresenter.getIndexList();
  }

  @override
  void setBannerList(data) {
    // TODO: implement setBannerList
    _controller.finishRefresh(success: true);
    HomeBannerEntity homeBannerEntity = new HomeBannerEntity().fromJson(data);
    if (homeBannerEntity.resCode == "1") {
      List<HomeBannerData> data = homeBannerEntity.data;
      bannerList.clear();
      bannerList.addAll(data);
      this.setState(() {
      });
    }
  }

  @override
  void setNewTruckList(data) {
    // TODO: implement setNewTruckList
    _controller.finishLoad();
    HomeNewtruckEntity homeNewtruckEntity = HomeNewtruckEntity().fromJson(data);
    if (homeNewtruckEntity.resCode == '1') {
      List<HomeNewtruckData> dataList = homeNewtruckEntity.data;
      this.setState(() {
        homeNewtruckDatas = dataList;
      });
    }
  }
}
